package io.kiki.task.server.common.rpc.server.handler;

import cn.hutool.core.net.url.UrlQuery;
import io.kiki.task.common.core.constant.SystemConstants.HTTP_PATH;
import io.kiki.task.common.core.model.HttpResponse;
import io.kiki.task.common.core.model.JobRequest;
import io.kiki.task.common.core.util.JsonUtil;
import io.kiki.task.server.common.handler.GetHttpRequestHandler;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import static io.kiki.task.common.core.constant.SystemConstants.BEAT.PONG;

/**
 * 接收心跳请求
 */
@Slf4j
@Component
public class BeatHttpRequestHandler extends GetHttpRequestHandler {

    @Override
    public boolean supports(String path) {
        return HTTP_PATH.BEAT.equals(path);
    }

    @Override
    public HttpMethod method() {
        return HttpMethod.POST;
    }

    @Override
    public String doHandler(String content, UrlQuery query, HttpHeaders headers) {
        log.debug("Beat check content:[{}]", content);
        JobRequest retryRequest = JsonUtil.parseObject(content, JobRequest.class);
        return JsonUtil.toJsonString(new HttpResponse(PONG, retryRequest.getReqId()));
    }
}
